/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018年1月20日 下午12:06:13
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */  
package com.itttq.gshop.admin.h5.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.itttq.gshop.domain.po.SysUserPo;
import com.itttq.gshop.generic.cmpt.annotation.Auth;
import com.itttq.gshop.generic.connector.ISysUserConnector;
import com.itttq.gshop.generic.controller.GenericController;
import com.itttq.gshop.generic.util.Constants;

/**   
 * @ClassName:  IndexController
 * @author: 迷蝶
 * @date:   2018年1月20日 下午12:06:13 
 *     
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
@RestController
public class IndexController extends GenericController {
	private static final long serialVersionUID = -6485190841367339L;
	@Autowired
	private ISysUserConnector userConnector;
	/**
	 * 主页，即登录页面
	 * @return
	 */
	@RequestMapping(value={"", "login.html"})
	public ModelAndView index() {
		return new ModelAndView("/login");
	}

	/**
	 * 登录
	 * @return
	 */
	@RequestMapping("user/login.html")
	public ModelAndView login(String username, String password, HttpServletRequest request) {
		SysUserPo userInfo = userConnector.login(username, password);
		ModelAndView mv = new ModelAndView("/index");
		if(null == userInfo) {
			mv.setViewName("/login");
			mv.addObject("msg", "用户名或密码错误！");
		} else {
			this.setSession(request, Constants.SESSION_USER, userInfo);
			// 如果有最后一次登录url记录，则跳转到最后一次登录地址
			Object obj = this.getSession(request, Constants.SESSION_URL_LAST);
			if(null != obj) {
				mv.setViewName("redirect:" + obj.toString());
			} else {
				mv.addObject("user", userInfo);
			}
		}
		return mv;
	}

	/**
	 * 首页页面
	 * @return
	 */
	@Auth
	@RequestMapping("index.html")
	public ModelAndView index(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/index");
		mv.addObject("user", this.getSession(request, Constants.SESSION_USER));
		return mv;
	}
}
